/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.analysis.ese;

import cz.mzk.oai.metadataRepository.analysis.ElementStatistics;
import cz.mzk.oai.metadataRepository.tools.FileTools;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.Text;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class EseElementStatistics extends ElementStatistics {

    private Map<String, Integer> valueTuplesOccurance;

    /**
     *
     * @param elementName name of the element. It must be one those in europeana:record.
     * For example dc:title, europeana:isShownBy
     */
    public EseElementStatistics(String elementName) {
        super();
        this.name = elementName;
        valueTuplesOccurance = new HashMap<String, Integer>(0);
    }

    /**
     * Loads the file, finds the element occurance(s) and updates internal counters accordingly
     * TODO: mozna zahrnout do natdtidy
     * @param file
     */
    public void process(File file) throws DocumentException, FileNotFoundException {
        Document document = FileTools.loadDocument(file);
        List<Node> nodes = document.selectNodes("//europeana:metadata/europeana:record/" + name + "/text()");
        int numberOfValues = 0;
        String valueTuple = null;
        for (Node node : nodes) {
            Text text = (Text) node;
            String value = text.getText();
            if (numberOfValues == 0) {
                valueTuple = value;
            } else {
                valueTuple = valueTuple + ", " + value;
            }
            numberOfValues++;
            updateOccurance(valueOccurance, value);
        }
        if (numberOfValues > 1) {
            updateOccurance(valueTuplesOccurance, valueTuple);
        }
        updateCounters(numberOfValues);
    }

    private void updateOccurance(Map<String, Integer> valueMap, String value) {
        Integer numberOfOccurence = valueMap.get(value);
        if (numberOfOccurence == null) {
            valueMap.put(value, Integer.valueOf(1));
        } else {
            Integer newNumberOfOccurence = Integer.valueOf(numberOfOccurence + 1);
            valueMap.put(value, newNumberOfOccurence);
        }
    }

    private void updateCounters(int numberOfValues) {
        if (numberOfValues == 0) {
            elementMissing++;
        } else {
            elementPresent++;
            if (numberOfValues == 1) {
                elementSingle++;
            } else {
                elementMultiple++;
            }
        }
    }

    public void printResults() {
        printResults(true);
    }

    public void printResults(boolean printValues) {
        System.out.println("datafield : " + name);
        System.out.println("records with element present: " + elementPresent);
        System.out.println("records with element missing: " + elementMissing);
        System.out.println("records with multiple elements: " + elementMultiple);
        System.out.println("records with single element: " + elementSingle);
        if (printValues) {
            System.out.println("values:");
            for (String value : valueOccurance.keySet()) {
                System.out.println(value + ":" + valueOccurance.get(value));
            }
            System.out.println("multiple values:");
            for (String value : valueTuplesOccurance.keySet()) {
                System.out.println(value + ":" + valueTuplesOccurance.get(value));
            }
        }
    }
}

